package com.jiang.myfm.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiang.myfm.R;
import com.jiang.myfm.adapters.AnchorAdapter;
import com.jiang.myfm.entities.anchor.AnchorEntity;
import com.jiang.myfm.interfaces.AnchorListener;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Findfragment_5 extends Fragment implements Callback<AnchorEntity> {
    private RecyclerView mRecyclerView;
    private AnchorListener mService;
    private AnchorEntity mAnchorEntity;
    private AnchorAdapter mAdapter;
    private int page;

    public Findfragment_5(){

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.layout_anchor,container,false);
        mRecyclerView= (RecyclerView) view.findViewById(R.id.recycler);

        loadData(page);
        mAdapter = new AnchorAdapter(mAnchorEntity,getContext());
        mRecyclerView.setAdapter(mAdapter);
        return view;
    }

    private void loadData(int page) {
        mService=new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(AnchorListener.class);
        mService.getAnchorEntity("android",page).enqueue(this);
    }

    @Override
    public void onResponse(Call<AnchorEntity> call, Response<AnchorEntity> response) {
        mAdapter.addAll(response.body());
    }

    @Override
    public void onFailure(Call<AnchorEntity> call, Throwable t) {

    }
}
